/**
 * 
 */
package com.cp16.tss.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;

import com.cp16.tss.common.Constants;
import com.cp16.tss.dto.Customer;
import com.cp16.tss.dto.Requirement;
import com.cp16.tss.form.RequirementForm;

/**
 * @author ADMIN
 *
 */
public class RequirementDAO {
	
	SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
	CustomerDAO customerDAO = new CustomerDAO();
	
	public Requirement getRequirementById(int requirementId) throws Exception {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Requirement requirement = (Requirement) session.get(Requirement.class, requirementId);
		session.getTransaction().commit();
		session.close();
		return requirement;
	}
	
	public List<Requirement> getAllRequirement() throws Exception {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Requirement.class);
		@SuppressWarnings("unchecked")
		List<Requirement> requirementList = criteria.list();
		session.getTransaction().commit();
		session.close();
		return requirementList;
	}
	
	public boolean addNewRequirement(Requirement requirement){
		try {
			Session session = sessionFactory.openSession();
			session.beginTransaction();
			session.save(requirement);
			session.getTransaction().commit();
			session.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
	}
	
	public boolean updateRequirement(Requirement requirement){
		try {
			Session session = sessionFactory.openSession();
			session.beginTransaction();
			session.update(requirement);
			session.getTransaction().commit();
			session.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Requirement> getRequirementByCustomerId(int customerId){
		Customer customer;
		List<Requirement> reqList = new ArrayList<Requirement>();
		try {
			customer = customerDAO.getCustomerById(customerId);
			Session session = sessionFactory.openSession();
			session.beginTransaction();
			reqList = session.createCriteria(Requirement.class).add(Restrictions.eq("customer", customer)).list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return reqList;
	}
	
	public boolean deleteRequirement(Requirement requirement) {
		boolean deleteReqFlag;
		try {
			Session session = sessionFactory.openSession();
			session.beginTransaction();
			session.delete(requirement);
			session.getTransaction().commit();
			session.close();
			deleteReqFlag = true;
		} catch (Exception e) {
			deleteReqFlag = false;
			e.printStackTrace();
		}
		return deleteReqFlag;
	}

	// TODO
	public List<Requirement> searchRequirements(RequirementForm requirementForm){
		String customerId = requirementForm.getTxtCustomerId().getText();
		String status = requirementForm.getCbStatus().getSelectedItem().toString();
		String dateType = requirementForm.getRdbDateChooser().getSelection().getSelectedObjects().toString();
		Date dateFrom = requirementForm.getTxtDateFrom().getDate();
		Date dateTo = requirementForm.getTxtDateTo().getDate();
		
		List<Requirement> reList = new ArrayList<Requirement>();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Requirement.class);
		if(Constants.BLANK.equals(customerId)){
			criteria.add(Restrictions.eq("customer.requirementId", customerId));
		}
		if(Constants.BLANK.equals(status)){
		}
		
		
		return reList;
	}
	
}
